////////////////////////////////////////////////////////////////////////////////////
// File:        Settings.cpp
// Purpose:     Static variables initialization
// Author:      Tukero
// Copyright:   (c) Tukero <tukero@gmail.com>
// Licence:     GNU/LGPL v3
////////////////////////////////////////////////////////////////////////////////////

#include "Settings.h"
#include <wx/filename.h>
#include <wx/fileconf.h>
#include <wx/msw/regconf.h>
#include <wx/wfstream.h>

TNodSettings* TNodSettings::ms_SettingsObj = NULL;
ConfigManager* TNodSettings::ms_ConfigManager = NULL;

wxString ConfigManager::m_fileFullPath = wxEmptyString;
ConfigManager* ConfigManager::m_config = NULL;
wxConfigBase* ConfigManager::m_base = NULL;
short ConfigManager::m_type = 0;

ConfigManager::ConfigManager()
{
    wxString strName;
    wxFileName fileName( Utils::GetExePath() );
    strName = fileName.GetName();
    fileName.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME );
    fileName.SetFullName( strName + wxT(".ini") );

    if ( fileName.FileExists() )
    {
        m_type = 2;
        m_fileFullPath = fileName.GetFullPath();

        wxFileInputStream fileInput( m_fileFullPath ) ;
        if ( fileInput.GetSize() <= 3 )
            m_base = new wxFileConfig( );
        else
        {
            wxLogNull logNo;
            m_base = new wxFileConfig( fileInput );
        }
    } 
    else
    {
        m_base = new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
        m_type = 1;
    }
}

void ConfigManager::Save()
{
    if ( m_type == 2 && m_base && !m_fileFullPath.IsEmpty() )
    {
        wxFileConfig *fileConfig = (wxFileConfig *)m_base;
        wxFileOutputStream file( m_fileFullPath );
        
        fileConfig->Save( file );
        file.Close();
    }
}